# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from tortoise import fields
from kernels.model import DbModel


class MusicRecordsModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    code = fields.CharField(null=False, default="", max_length=32, description="编号")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    task_id = fields.CharField(null=False, default="", max_length=100, description="片段ID")
    engine = fields.CharField(null=False, default="", max_length=100, description="音乐引擎: [suno]")
    channel = fields.CharField(null=False, default="", max_length=100, description="音乐渠道: [openai_hk]")
    mode = fields.CharField(null=False, default="", max_length=10, description="歌曲模式: [灵感=muse, 歌词=lyric]")
    title = fields.CharField(null=False, default="", max_length=200, description="歌曲标题")
    prompt = fields.TextField(null=False, description="歌曲描述")
    tags = fields.CharField(null=False, default="", max_length=200, description="歌曲风格")
    mv = fields.CharField(null=False, default="", max_length=100, description="生成版本")
    make_instrumental = fields.SmallIntField(null=False, default=0, description="纯的音乐: [0=否, 1=是]")
    cover_url = fields.CharField(null=False, default="", max_length=250, description="音乐封面")
    audio_url = fields.CharField(null=False, default="", max_length=250, description="音频地址")
    duration = fields.IntField(null=False, default=0, description="时长(秒)")
    lyric = fields.TextField(null=False, description="歌曲歌词")
    use_points = fields.IntField(null=False, default=0, description="消耗积分")
    retry_count = fields.IntField(null=False, default=0, description="重试次数")
    fail_reason = fields.TextField(null=False, description="失败原因")
    notify_snap = fields.TextField(null=False, description="通知快照")
    ip = fields.CharField(null=False, default="", max_length=100, description="来源IP")
    status = fields.SmallIntField(null=False, default=0, description="生成状态: [0=等待中, 1=执行中, 2=成功, 3=失败]")
    is_free = fields.SmallIntField(null=False, default=0, description="会员免费: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    start_time = fields.IntField(null=False, default=0, description="开始时间")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "音乐记录表"
        table = DbModel.table_prefix("music_records")


class MusicStylesModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    name = fields.CharField(null=False, max_length=100, default="", description="名称")
    value = fields.CharField(null=False, max_length=100, default="", description="名称")
    image = fields.CharField(null=False, max_length=250, default="", description="图标")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    is_disable = fields.SmallIntField(null=False, default=0, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "音乐风格表"
        table = DbModel.table_prefix("music_styles")


class MusicSquareModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    source = fields.IntField(null=False, default=0, description="来源: [1=官方, 2=用户]")
    operate_id = fields.IntField(null=False, default=0, description="操作人ID")
    cate_id = fields.IntField(null=False, default=0, description="分类ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    music_code = fields.CharField(null=False, default="", max_length=32, description="音乐编号")
    cover_url = fields.CharField(null=False, default="", max_length=250, description="音乐封面")
    audio_url = fields.CharField(null=False, default="", max_length=250, description="音乐地址")
    title = fields.CharField(null=False, default="", max_length=200, description="音乐标题")
    tags = fields.TextField(default="", description="音乐风格")
    prompt = fields.TextField(default="", description="音乐描述")
    lyric = fields.TextField(default="", description="音乐歌词")
    duration = fields.IntField(null=False, default=0, description="音乐时长")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    public_sum = fields.IntField(null=False, default=1, description="公开次数")
    audit_result = fields.CharField(null=False, max_length=300, default="", description="审核结果")
    audit_status = fields.SmallIntField(null=False, default=0, description="审核状态: [0=待审, 1=审核通过, 2=审核拒绝]")
    is_enable = fields.SmallIntField(null=False, default=0, description="是否显示: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "音乐广场表"
        table = DbModel.table_prefix("music_square")

    @classmethod
    async def querySquareDictsByRecordId(cls, ids: list, field: list = None) -> dict:
        """ 获取广场字典 """
        dicts = {}
        if ids:
            field = cls.without_field("is_delete,delete_time") if not field else field
            result = await (cls.filter(record_id__in=set(ids), is_delete=0).all().values(*field))
            dicts = {item[str("record_id")]: item for item in result}
        return dicts


class MusicCollectModel(DbModel):
    id = fields.IntField(pk=True, description="主键")
    user_id = fields.IntField(null=False, default=0, description="用户ID")
    record_id = fields.IntField(null=False, default=0, description="记录ID")
    total_num = fields.IntField(null=False, default=0, description="操作次数")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "音乐收藏表"
        table = DbModel.table_prefix("music_collect")

